﻿
''' <summary>
''' Utilities Module : Contains Common Task and Routines.
''' </summary>
Public Module Utilities
    
    ''' <summary>
    ''' Gets a string representing a given size.
    ''' </summary>
    ''' <param name="size">Item size for 'Long' types</param>
    Public Function StringFromSize(ByVal size As Long) As String
        Dim s As String = ""
        Select Case size
            Case Is >= 1073741824
                s = String.Format("{0:F3} G", Math.Round(size / 1073741824, 3))
            Case Is >= 1048576
                s = String.Format("{0:F3} M", Math.Round(size / 1048576, 3))
            Case Is >= 1024
                s = String.Format("{0:F3} K", Math.Round(size / 1024, 3))
            Case Else
                s = String.Format("{0:F3}", size)
        End Select
        Return s
    End Function

    ''' <summary>
    ''' Gets a string representing a given size.
    ''' </summary>
    ''' <param name="size">Item size for 'Double' types</param>
    Public Function StringFromSize(ByVal size As Double) As String
        Dim s As String = ""
        Select Case size
            Case Is >= 1073741824
                s = String.Format("{0:F3} G", Math.Round(size / 1073741824, 3))
            Case Is >= 1048576
                s = String.Format("{0:F3} M", Math.Round(size / 1048576, 3))
            Case Is >= 1024
                s = String.Format("{0:F3} K", Math.Round(size / 1024, 3))
            Case Else
                s = String.Format("{0:F3}", size)
        End Select
        Return s
    End Function

    ''' <summary>
    ''' Gets a string representing a given size.
    ''' </summary>
    ''' <param name="size">Item size for 'Double' types</param>
    ''' <param name="precision">Desired precision digits</param>
    Public Function StringFromSize(ByVal size As Double, ByVal precision As Integer) As String
        Dim s As String = ""
        Select Case size
            Case Is >= 1073741824
                s = String.Format("{0} G", Math.Round(size / 1073741824, precision))
            Case Is >= 1048576
                s = String.Format("{0} M", Math.Round(size / 1048576, precision))
            Case Is >= 1024
                s = String.Format("{0} K", Math.Round(size / 1024, precision))
            Case Else
                s = String.Format("{0}", size)
        End Select
        Return s
    End Function

    ''' <summary>
    ''' Gets an image representing a given Zip Type.
    ''' </summary>
    ''' <param name="ziptype">Zip Type</param>
    <Extension()>
    Public Function ZipImage(ByVal ziptype As ZipType) As Image
        Dim img As Image = Nothing
        If ziptype = ziptype.SimpleZip Then
            img = My.Resources.Img_SimpleZip
        ElseIf ziptype = ziptype.SplitZip Then
            img = My.Resources.Img_SplitZip
        ElseIf ziptype = ziptype.SfxZip Then
            img = My.Resources.Img_SfxZip
        End If
        Return img
    End Function

    ''' <summary>
    ''' Gets an image key associated to a given Zip type.
    ''' </summary>
    ''' <param name="ziptype">Zip Type</param>
    <Extension()>
    Public Function ImageKey(ByVal ziptype As ZipType) As String
        Dim key As String = ""
        Select Case ziptype
            Case ziptype.SimpleZip
                key = "simplezip"
            Case ziptype.SplitZip
                key = "splitzip"
            Case ziptype.SfxZip
                key = "sfxzip"
        End Select
        Return key
    End Function

    ''' <summary>
    ''' Gets a file extension representing a given Zip type.
    ''' </summary>
    ''' <param name="ziptype">Zip Type</param>
    <Extension()>
    Public Function Extension(ByVal ziptype As ZipType) As String
        Dim ext As String = ""
        If ziptype = ziptype.SfxZip Then
            ext = ".exe"
        Else
            ext = ".zip"
        End If
        Return ext
    End Function

    ''' <summary>
    ''' Gets a string representing a given compression method.
    ''' </summary>
    ''' <param name="method">Compression method</param>
    <Extension()>
    Public Function InfosString(ByVal method As Zip.CompressionMethod) As String
        Dim ms As String = ""
        Select Case method
            Case Zip.CompressionMethod.None
                ms = "None"
            Case Zip.CompressionMethod.Deflate
                ms = "Deflate"
            Case Zip.CompressionMethod.BZip2
                ms = "BZip2"
        End Select
        Return "Method = " & ms
    End Function

    ''' <summary>
    ''' Gets a 0 based index representing a given compression method.
    ''' </summary>
    ''' <param name="method">Compression method</param>
    <Extension()>
    Public Function ToIndex(ByVal method As Zip.CompressionMethod) As Integer
        Dim index As Integer = -1
        Select Case method
            Case Zip.CompressionMethod.None
                index = 0
            Case Zip.CompressionMethod.Deflate
                index = 1
            Case Zip.CompressionMethod.BZip2
                index = 2
        End Select
        Return index
    End Function

    ''' <summary>
    ''' Gets a string representing a given compression level.
    ''' </summary>
    ''' <param name="level">Compression Level</param>
    <Extension()>
    Public Function InfosString(ByVal level As Zlib.CompressionLevel) As String
        Dim ms As String = ""
        Select Case level
            Case Zlib.CompressionLevel.None
                ms = "None (0)"
            Case Zlib.CompressionLevel.BestSpeed
                ms = "Best Speed (1)"
            Case Zlib.CompressionLevel.Default
                ms = "Default (6)"
            Case Zlib.CompressionLevel.BestCompression
                ms = "Best Compression (9)"
            Case Else
                ms = "(" & CInt(level).ToString & ")"
        End Select
        Return "Level = " & ms
    End Function

    ''' <summary>
    ''' Gets a string representing a given compression strategy.
    ''' </summary>
    ''' <param name="strategy">Compression strategy</param>
    <Extension()>
    Public Function InfosString(ByVal strategy As Zlib.CompressionStrategy) As String
        Dim ms As String = ""
        Select Case strategy
            Case Zlib.CompressionStrategy.Default
                ms = "Default"
            Case Zlib.CompressionStrategy.Filtered
                ms = "Filtered"
            Case Zlib.CompressionStrategy.HuffmanOnly
                ms = "Huffman"
        End Select
        Return "Strategy = " & ms
    End Function

    ''' <summary>
    ''' Gets a string representing a given text encoding.
    ''' </summary>
    ''' <param name="encod">Text encoding</param>
    <Extension()>
    Public Function InfosString(ByVal encod As System.Text.Encoding) As String
        Return "Encoding = " & encod.BodyName
    End Function

    ''' <summary>
    ''' Gets a string representing a given encoding usage.
    ''' </summary>
    ''' <param name="encodusage">Encoding usage</param>
    <Extension()>
    Public Function InfosString(ByVal encodusage As Zip.ZipOption) As String
        Dim ms As String = ""
        Select Case encodusage
            Case Zip.ZipOption.Never
                ms = "Never"
            Case Zip.ZipOption.AsNecessary
                ms = "As Necessary"
            Case Zip.ZipOption.Always
                ms = "Always"
        End Select
        Return "Encoding Usage = " & ms
    End Function

    ''' <summary>
    ''' Gets a string representing a given Zip64 Usage.
    ''' </summary>
    ''' <param name="zip64usage">Zip64 Usage</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function InfosString(ByVal zip64usage As Zip.Zip64Option) As String
        Dim ms As String = ""
        Select Case zip64usage
            Case Zip.Zip64Option.Never
                ms = "Never"
            Case Zip.Zip64Option.AsNecessary
                ms = "As Necessary"
            Case Zip.Zip64Option.Always
                ms = "Always"
        End Select
        Return "Zip64 Usage = " & ms
    End Function

    ''' <summary>
    ''' Gets a string representing a given encryption algorithm.
    ''' </summary>
    ''' <param name="algo">Encryption algorithm</param>
    <Extension()>
    Public Function InfosString(ByVal algo As Zip.EncryptionAlgorithm) As String
        Dim ms As String = ""
        Select Case algo
            Case Zip.EncryptionAlgorithm.None
                ms = "None"
            Case Zip.EncryptionAlgorithm.PkzipWeak
                ms = "PKzip"
            Case Zip.EncryptionAlgorithm.WinZipAes128
                ms = "WinZipAes128"
            Case Zip.EncryptionAlgorithm.WinZipAes256
                ms = "WinZipAes256"
        End Select
        Return ms
    End Function

    ''' <summary>
    ''' Gets the default Sfx Zip Type's icon path.
    ''' </summary>
    Public Function DefaultSfxIcon() As String
        Dim ip As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "Icon_Sfx.ico")
        If IO.File.Exists(ip) = False Then
            My.Resources.Icon_Sfx.Save(New IO.FileStream(ip, IO.FileMode.Create))
        End If
        Return ip
    End Function

    ''' <summary>
    ''' Gets a unique string representing a given TimeSpan as 0000000000.
    ''' </summary>
    ''' <param name="ts">TimeSpan value</param>
    <Extension()>
    Public Function UniqueString(ByVal ts As TimeSpan) As String
        Return String.Format("{0:D2}{1:D2}{2:D2}{3:D4}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds)
    End Function

    ''' <summary>
    ''' Gets a string representing a given TimeSpan as 00:00:00
    ''' </summary>
    ''' <param name="ts">TimeSpan value</param>
    <Extension()>
    Public Function ValueString(ByVal ts As TimeSpan) As String
        Return String.Format("{0:D2}:{1:D2}:{2:D2}", ts.Hours, ts.Minutes, ts.Seconds)
    End Function

    ''' <summary>
    ''' Creates and gets a Temporary Folder path.
    ''' </summary>
    Public Function ZipNodeTempFolder() As String
        Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, DateTime.Now.TimeOfDay.UniqueString)
        IO.Directory.CreateDirectory(path)
        Return path
    End Function

End Module